package com.stenio.fancat.controller.shop;

import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.shop.FcCategory;
import com.stenio.fancat.form.shop.CateogryForm;
import com.stenio.fancat.service.shop.CategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangqiu
 * @date 2018/12/1
 * @desc
 **/
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加品类
     * @param cateogryForm
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public Result add(@RequestBody CateogryForm cateogryForm){
        Result result = new Result();
        result.setData(categoryService.saveCategory(cateogryForm));
        return result;
    }

    /**
     * 更新品类信息
     * @param cateogryForm
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public Result update(@RequestBody CateogryForm cateogryForm){
        Result result = new Result();
        result.setData(categoryService.updateCategory(cateogryForm));
        return result;
    }

    /**
     * 删除品类
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable Integer id){
        Result result = new Result();
        int i = categoryService.deleteById(id);
        if(i<1) {
            result.setFlag(Result.FLAG.failure);
        }
        return result;
    }

    /**
     * 根据父级查询
     * @param parentId
     * @return
     */
    @GetMapping("findByParentId/{parentId}")
    @ResponseBody
    public Result<List<FcCategory>> findByParentId(@PathVariable Integer parentId){
        Result<List<FcCategory>> result = new Result<>();
        result.setData(categoryService.findByParentId(parentId));
        return result;
    }

    /**
     * 查询所有二级分类
     * @return
     */
    @PostMapping("findSecondaryAll")
    @ResponseBody
    public Result<List<FcCategory>> findSecondaryAll(){
        Result<List<FcCategory>> result = new Result<>();
        List<FcCategory> secondaryAll = categoryService.findSecondaryAll();
        if(CollectionUtils.isNotEmpty(secondaryAll)) {
            result.setData(secondaryAll);
        }else {
            result.setFlag(Result.FLAG.failure);
        }
        return result;
    }


}
